package com.ff.xyh.consumer.dictionary.controller;


import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.dictionary.Type;
import com.ff.xyh.consumer.dictionary.service.TypeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 帖子类型表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-26
 */
@Api(tags = "类型表")
@RestController
@RequestMapping("/dictionary")
public class TypeController {
    @Autowired
    private TypeService typeService;

    @GetMapping("get-type-all")
    public ResultObject getAllType() {
        List<Type> list = typeService.list(null);
        if (list == null || list.isEmpty()){
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            Map map = new HashMap();
            map.put("items", list);
            return ResultObject.ok().data(map);
        }
    }

    @GetMapping("get-type-id")
    public ResultObject getTypeById(
            @RequestParam(name = "id") Integer id) {
        Type type = typeService.getById(id);
        if (type == null){
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            Map map = new HashMap();
            map.put("data", type);
            return ResultObject.ok().data(map);
        }
    }

    @GetMapping("delete-type-id")
    public ResultObject deleteTypeById(
            @RequestParam(name = "id") int id) {
        Type type = typeService.getById(id);
        if (type == null){
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (typeService.removeById(id)) {
                Map map = new HashMap();
                map.put("data", type);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        }
    }

    @PostMapping("insert-type")
    public ResultObject insertType (
            @RequestBody Type type) {
        if (typeService.getById(type.getId()) != null){
            return ResultObject.error().message(ResultMessage.OBJECT_ALREADY_EXISTS);
        } else {
            if (typeService.save(type)) {
                Map map = new HashMap();
                map.put("data", type);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT_BUT_FAILED_TO_INSERT);
            }
        }
    }

    @PostMapping("update-type")
    public ResultObject updateType(
            @RequestBody Type type) {
        if (typeService.getById(type.getId()) == null){
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (typeService.updateById(type)) {
                Map map = new HashMap();
                map.put("data", type);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_UPDATE);
            }
        }
    }
}

